/* -------------------------------------------------------------------------------- */
/* -- µGUI - Generic GUI module (C)Achim Döbler, 2015                            -- */
/* -------------------------------------------------------------------------------- */
// µGUI is a generic GUI module for embedded systems.
// This is a free software that is open for education, research and commercial
// developments under license policy of following terms.
//
//  Copyright (C) 2015, Achim Döbler, all rights reserved.
//  URL: http://www.embeddedlightning.com/
//
// * The µGUI module is a free software and there is NO WARRANTY.
// * No restriction on use. You can use, modify and redistribute it for
//   personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
// * Redistributions of source code must retain the above copyright notice.
//
/* ------------------------------------------------------------------------------- */
#ifndef __UGUI_CORE_H__
#define __UGUI_CORE_H__

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include "ugui_config.h"
#include "ugui_color.h"
#include <stdint.h>

/* -------------------------------------------------------------------------------- */
/* -- µGUI FONTS                                                                 -- */
/* -- Source: http://www.mikrocontroller.net/user/show/benedikt                  -- */
/* -------------------------------------------------------------------------------- */
typedef enum { FONT_TYPE_1BPP, FONT_TYPE_8BPP } FONT_TYPE;

typedef struct {
  unsigned char* p;
  FONT_TYPE font_type;
  int16_t char_width;
  int16_t char_height;
  uint16_t start_char;
  uint16_t end_char;
  uint8_t* widths;
} UG_FONT;

typedef int8_t UG_RESULT;

#ifdef USE_UI_WIDGETS
#include "ugui_widgets.h"
#endif

#ifndef NULL
#define NULL ((void*)0)
#endif

/* -- FUNCTION RESULTS                                                           -- */
#define UG_RESULT_FAIL -1
#define UG_RESULT_OK   0

/* -- µGUI DRIVER                                                                -- */
typedef struct {
  void* driver;
  uint8_t state;
} UG_DRIVER;

#define DRIVER_REGISTERED (1 << 0)
#define DRIVER_ENABLED    (1 << 1)

/* Supported drivers */
#define NUMBER_OF_DRIVERS 3
#define DRIVER_DRAW_LINE  0
#define DRIVER_FILL_FRAME 1
#define DRIVER_FILL_AREA  2

/* uGUI BMP */
#define BMP_BPP_1  (1)
#define BMP_BPP_8  (8)
#define BMP_BPP_16 (16)
#define BMP_BPP_24 (24)
#define BMP_BPP_32 (32)

#define BMP_COLOR_565 (1 << 0)
#define BMP_COLOR_888 (1 << 2)

typedef struct {
  void* pdata;
  uint16_t width;
  uint16_t height;
  uint16_t bpp;    //!< 1,8,16,32
  uint8_t colors;  //!< not support yet,use the default 565 and 888
  /* data */
} UG_BMP;

/* -- µGUI CORE STRUCTURE                                                        -- */
typedef struct {
  void (*pset)(int16_t, int16_t, UG_COLOR);
  int16_t x_dim;
  int16_t y_dim;
// UG_TOUCH touch;
#ifdef USE_UI_WIDGETS
  UG_WINDOW* next_window;
  UG_WINDOW* active_window;
  UG_WINDOW* last_window;
  UG_COLOR desktop_color;
#endif /* USE_UI_WIDGETS */

#ifdef USE_SCREEN_CONSOLE
  struct {
    int16_t x_pos;
    int16_t y_pos;
    int16_t x_start;
    int16_t y_start;
    int16_t x_end;
    int16_t y_end;
    UG_COLOR fore_color;
    UG_COLOR back_color;
  } console;
#endif /* USE_CONSOLE */

  UG_FONT font;
  int8_t char_h_space;
  int8_t char_v_space;
  UG_COLOR fore_color;
  UG_COLOR back_color;
  uint8_t state;
  UG_DRIVER driver[NUMBER_OF_DRIVERS];
} UG_GUI;

#define UG_SATUS_WAIT_FOR_UPDATE (1 << 0)

/* -------------------------------------------------------------------------------- */
/* -- PROTOTYPES                                                                 -- */
/* -------------------------------------------------------------------------------- */
/* Classic functions */
int16_t UG_Init(UG_GUI* g, void (*p)(int16_t, int16_t, UG_COLOR), int16_t x, int16_t y);
int16_t UG_SelectGUI(UG_GUI* g);
void UG_FontSelect(const UG_FONT* font);
void UG_FillScreen(UG_COLOR c);
void UG_FillFrame(int16_t x1, int16_t y1, int16_t x2, int16_t y2, UG_COLOR c);
void UG_FillRoundFrame(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t r, UG_COLOR c);
void UG_DrawMesh(int16_t x1, int16_t y1, int16_t x2, int16_t y2, UG_COLOR c);
void UG_DrawFrame(int16_t x1, int16_t y1, int16_t x2, int16_t y2, UG_COLOR c);
void UG_DrawRoundFrame(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t r, UG_COLOR c);
void UG_DrawPixel(int16_t x0, int16_t y0, UG_COLOR c);
void UG_DrawCircle(int16_t x0, int16_t y0, int16_t r, UG_COLOR c);
void UG_FillCircle(int16_t x0, int16_t y0, int16_t r, UG_COLOR c);
void UG_DrawArc(int16_t x0, int16_t y0, int16_t r, uint8_t s, UG_COLOR c);
void UG_DrawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, UG_COLOR c);
void UG_PutString(int16_t x, int16_t y, char* str);
void UG_PutChar(char chr, int16_t x, int16_t y, UG_COLOR fc, UG_COLOR bc);
void UG_ConsolePutString(char* str);
void UG_ConsoleSetArea(int16_t xs, int16_t ys, int16_t xe, int16_t ye);
void UG_ConsoleSetForecolor(UG_COLOR c);
void UG_ConsoleSetBackcolor(UG_COLOR c);
void UG_SetForecolor(UG_COLOR c);
void UG_SetBackcolor(UG_COLOR c);
int16_t UG_GetXDim(void);
int16_t UG_GetYDim(void);
void UG_FontSetHSpace(uint8_t s);
void UG_FontSetVSpace(uint8_t s);
void UG_DrawBMP(int16_t x, int16_t y, UG_BMP* bmp);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
